// Extra/pages/MessageNotice/index.js
import { fetchWarningList } from '@/services/warning';
import { customNavigate } from '@/utils/route';
import { showToast } from '@/utils/util';
import dayjs from 'dayjs';
const app = getApp();
let page_num = 1;
const deviceTypeMap = {
    '0': '腕表',
    '1': '燃气报警器',
    '2': '跌倒监测守护仪',
    '4': '一键报警器按键',
    '5': '独立式一键报警器',
    '6': '报警网关',
    '7': '一键报警器按键',
    '8': '血压计',
    '9': '血糖仪',
    '13': '人体存在感知仪',
    '14': '睡眠监测守护仪'
}

Page({
    data: {
        staticPrefix: app.globalData.STATIC_PREFIX,
        deviceTypeValue: '1000',
        deviceTypeOptions: app.globalData.DEVICE_TYPE_OPTIONS,
        alarmValue: null,
        alarmOptions: [],
        itemTitle: '筛选',
        minDate: dayjs().subtract(2, 'month').valueOf(),
        maxDate: new Date().getTime(),
        start_time: undefined,
        end_time: undefined,
        active: 'b',
        warn_list: []
    },
    onLoad(options) {
    },
    onShow() {
        this.setData({
            alarmOptions: app.globalData.ALARM_OPTIONS[this.data.deviceTypeValue],
            alarmValue: '全部'
        }, this.searchList)
    },
    onChange(e) {
        const {
            type
        } = e.currentTarget.dataset, that = this;
        console.log(type)
        this.setData({
            warn_list: []
        }, () => {
            page_num = 1;
            if (type == 'deviceTypeValue') {
                this.setData({
                    alarmOptions: app.globalData.ALARM_OPTIONS[e.detail],
                    alarmValue: '全部',
                    deviceTypeValue: e.detail
                }, this.searchList)
            } else if (type == 'alarmOptions') {
                this.setData({
                    alarmValue: e.detail
                }, this.searchList)
            }
        })
    },

    async searchList() {
        const {
            deviceTypeValue,
            alarmValue,
            begin_time,
            over_time
        } = this.data;
        const userInfo = wx.getStorageSync('userInfo');
        const params = {
            page_num,
            page_size: 10,
            device_type: deviceTypeValue,
            warn_type: alarmValue,
            id: userInfo.id,
            ...(begin_time && over_time && {
                begin_time,
                over_time
            })
        };
        const {
            return_code,
            data
        } = await fetchWarningList(params);
        if (return_code === 'SUCCESS') {
            this.setData({
                trigger: false
            }, () => {
                if (data.length === 0) {
                    page_num--;
                } else {
                    this.setData({
                        warn_list: [...this.data.warn_list, ...data].map((item) => {
                            return {
                                ...item,
                                device_type: deviceTypeMap[item.deviceType]
                            }
                        })
                    });
                }
            });
        }
    },
    onListRefresh() {
        this.setData({
            warn_list: [],
            trigger: true
        }, () => {
            page_num = 1;
            this.searchList()
        })
    },
    onListLoad() {
        page_num += 1;
        this.searchList()
    },
    onConfirm() {
        const component  = this.selectComponent('.calendar');
        if(component.data.currentDate.length < 2 || !component.data.currentDate[1]) {
            showToast('请选择正确的时间范围！')
            return;
        };
        this.setData({
            begin_time: dayjs(this.selectComponent('.calendar').data.currentDate[0]).format('YYYY-MM-DD'),
            over_time: dayjs(this.selectComponent('.calendar').data.currentDate[1]).format('YYYY-MM-DD')
        }, () => {
            this.selectComponent('#item').toggle();
            this.onListRefresh()
        })
    },
    onClose() {
        this.selectComponent('#item').toggle();
    },
    onClick(e) {
        customNavigate({
            url: './Info/index',
            params: {
                ...e.currentTarget.dataset.info
            }
        })
    },
    onHide() {
        page_num = 1;
        this.setData({
            warn_list: []
        })
    }
})